package com.dbl.oms.platform.facade.handler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dbl.oms.common.exception.BusinessException;
import com.dbl.oms.common.model.ResultData;

import lombok.extern.slf4j.Slf4j;

/**
 * 统一异常处理
 */
@Slf4j
@ControllerAdvice
public class GlobalDefaultExceptionHandler {
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public ResultData exceptionHandler(Exception ex, HttpServletResponse response) throws Exception {

		ResultData rs = new ResultData();
		rs.setMsg("系统繁忙，请稍后再试！");
		if (ex instanceof BusinessException) {
			BusinessException be = (BusinessException) ex;
			rs.setCode(be.getCode());
			rs.setMsg(be.getMessage());
			log.warn("", ex);
		} else if (ex instanceof ServletException) {
			log.error("", ex);
			throw ex;
		}
		return rs;
	}
}
